""" 

    Static.py used in RepresentedBy Facebook application
    http://apps.facebook.com/representedby/
        
    This page is just a wrapper page for the simpler pages (Invite, Credits, Location) which 
    just render a basic static template and don't require much logic. 
    
"""

__author__ = "Will Turnage (will @t wubbahed d0t com)"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2009 wubbahed.com"
__license__ = "GPLv3"

import os
import wsgiref.handlers
import facebook

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from utils import fbUtil

class Goodies(webapp.RequestHandler):
    def post(self):
        info = {"section": "goodies", "requireLogin": True, "template": "../templates/goodies.fbml" }
        output = PageCreator()
        output.render(self, info)
                   
class Legislators(webapp.RequestHandler):
    def post(self):
        info = {"section": "legislators", "requireLogin": False, "template": "../templates/legislators.fbml" }
        output = PageCreator()
        output.render(self, info)
               
class Location(webapp.RequestHandler):
    def post(self):
        info = {"section": "location", "requireLogin": True, "template": "../templates/gmap.fbml", "myLocation": True}
        output = PageCreator()
        output.render(self, info)
              
class Invite(webapp.RequestHandler):
    def post(self):
        info = {"section": "invite", "requireLogin": True, "template": "../templates/invite.fbml", "appUsers": True}
        output = PageCreator()
        output.render(self, info)

class Credits(webapp.RequestHandler):
    def post(self):
        info = {"section": "credits", "requireLogin": False, "template": "../templates/credits.fbml" }
        output = PageCreator()
        output.render(self, info)
           
           
class PageCreator():
    def render(self, webapp, info=None):
        try: 
            if info is not None:
                if info.has_key("requireLogin"):
                    if info["requireLogin"]:
                         
                        #check if Facebook app is installed
                        webapp.facebookapi = fbUtil().checkFacebookAppInstalled(webapp, True, info["section"])
                        if webapp.facebookapi is None:
                            return
                        
                        if info.has_key("appUsers"):
                            info["appUserFriends"] = webapp.facebookapi.friends.getAppUsers()
                            
                        if info.has_key("myLocation"):
                            info["uid"] = webapp.facebookapi.uid
                            user = webapp.facebookapi.users.getInfo([webapp.facebookapi.uid], ['uid', 'current_location'])[0]
                            info["current_location"] = user["current_location"]
                                
                path = os.path.join(os.path.dirname(__file__), '../templates/heading.fbml')
                webapp.response.out.write(template.render(path, info))
                path = os.path.join(os.path.dirname(__file__), info["template"])
                webapp.response.out.write(template.render(path, info))
                
        except facebook.FacebookError, e:
            webapp.response.out.write("Sorry, Facebook ain't working - " + str(e.msg))
                

def main():
    application = webapp.WSGIApplication([('/fb/goodies', Goodies), ('/fb/legislators', Legislators), ('/fb/location', Location), ('/fb/invite', Invite), ('/fb/credits', Credits)],
                                       debug=True)
    run_wsgi_app(application)
                                       
#this loads the custome filters that are used in some templates
webapp.template.register_template_library('customfilters')
if __name__ == '__main__':
    main()
